declare let AMapUI: any;
declare let AMap: any;
module MC.View {
	export class Smap {
		public constructor() {
			MC.loadJsUrl("//webapi.amap.com/maps?v=1.4.15&key=d2f351ff3f32cd4eb855d548323127e8&plugin=AMap.ToolBar", () => {
				console.log("ToolBar");
				MC.loadJsUrl("//webapi.amap.com/ui/1.0/main.js?v=1.0.11", () => {
					console.log("main");
				///	this.init();
				});
			});
		}
		private container: JQuery = $(`
		<div class="containerDiv">
		 	<div class="TitleTop">
				<b class="b_title">地址选择</b>
				<i class="return"></i>
           	  <a class="button button_ok right_ok">确定</a>
    		</div>
			<div id="container" class="container"></div>
			<div class="MapDivSelect">
				<div class="addressJq"></div>

			</div>
		</div>
		`);
		private position: any = null;
		private positionStr:string='';
		private address: string = '';
		private addressJq: JQuery = null;
		private formattedAddress:string='';
		private zoom:string='';
		private positionPicker:any=null;
		private map:any=null;
		public Select(fun:Function,position:string='') {
			let mthis = this;
			$('body').append(this.container);
			this.addressJq = this.container.find('.addressJq');
			this.container.find('.TitleTop .return').on('click',()=>{
				this.close();
			});
			this.container.find('.TitleTop .right_ok').on('click',()=>{
				this.close();
				fun({
					position:mthis.position,
					address:mthis.address,
					formattedAddress:mthis.formattedAddress,
					positionStr:mthis.positionStr,
					zoom:mthis.zoom
				});
			});
			AMapUI.loadUI(['misc/PositionPicker'], function (PositionPicker: any) {
				let MapPa:any={
					zoom: 16,
					scrollWheel: true
				};
				if(position&&position!==''){
					let pos=JSON.parse(position);
					MapPa.center=[pos.lng,pos.lat];
					if(pos.zoom&&pos.zoom!==''){
						MapPa.zoom=pos.zoom;
					}
				} 
			mthis.map = new AMap.Map('container',MapPa)
			mthis.positionPicker = new PositionPicker({
					mode: 'dragMap',
					map: mthis.map
				});

				mthis.positionPicker.on('success', function (positionResult: any) {
					/// 	document.getElementById('lnglat').innerHTML = positionResult.position;
					mthis.addressJq.text(positionResult.address);
					mthis.position = positionResult.position;
					mthis.address = positionResult.address;
					mthis.position.zoom=mthis.map.getZoom();
					mthis.positionStr=JSON.stringify(mthis.position);
					mthis.formattedAddress=positionResult.regeocode.addressComponent.country+' '+
					positionResult.regeocode.addressComponent.province+' '+
					positionResult.regeocode.addressComponent.city+' '+
					positionResult.regeocode.addressComponent.district+' '+
					positionResult.regeocode.addressComponent.township+' ';
					mthis.zoom=mthis.map.getZoom();
				});
				mthis.positionPicker.on('fail', function (positionResult: any) {
					/*	document.getElementById('lnglat').innerHTML = ' ';
						document.getElementById('address').innerHTML = ' ';
						*/
					MC.msg.alert("地图初始化错误:" + positionResult);
				});
				/*	var onModeChange = function (e:any) {
						positionPicker.setMode(e.target.value)
					}*/
				/*	var startButton = document.getElementById('start');
					var stopButton = document.getElementById('stop');*/
				//	var dragMapMode = document.getElementsByName('mode')[0];
				///	var dragMarkerMode = document.getElementsByName('mode')[1];
				/*	AMap.event.addDomListener(startButton, 'click', function () {
						positionPicker.start(map.getBounds().getSouthWest())
					})
					AMap.event.addDomListener(stopButton, 'click', function () {
						positionPicker.stop();
					})*/
				//	AMap.event.addDomListener(dragMapMode, 'change', onModeChange)
				//	AMap.event.addDomListener(dragMarkerMode, 'change', onModeChange);
				mthis.positionPicker.start();
				mthis.map.panBy(0, 1);

				mthis.map.addControl(new AMap.ToolBar({
					liteStyle: true
				}))
			});
		}
		 public close(){
			 this.container.remove();
		 }
	}
}